/*     */ package com.wzh.action.tag;
/*     */ 
/*     */ import com.opensymphony.xwork2.ActionContext;
/*     */ import com.wzh.bean.PageIndex;
/*     */ import com.wzh.bean.PageView;
/*     */ import com.wzh.bean.QueryResult;
/*     */ import com.wzh.bean.book.BookInfo;
/*     */ import com.wzh.service.bookinfo.BookInfoService;
/*     */ import com.wzh.utils.PropertiesUtil;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Iterator;
/*     */ import java.util.LinkedHashMap;
/*     */ import java.util.List;
/*     */ import javax.servlet.ServletResponse;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.jsp.JspException;
/*     */ import javax.servlet.jsp.PageContext;
/*     */ import javax.servlet.jsp.tagext.TagSupport;
/*     */ import org.apache.struts2.ServletActionContext;
/*     */ import org.springframework.web.context.WebApplicationContext;
/*     */ import org.springframework.web.context.support.WebApplicationContextUtils;
/*     */ 
/*     */ public class BookTag extends TagSupport
/*     */ {
/*     */   private static final long serialVersionUID = 1L;
/*  28 */   private int num = 10;
/*  29 */   private Integer booktype = Integer.valueOf(0);
/*     */   private String order;
/*     */   private String var;
/*  32 */   private int start = 0;
/*  33 */   private int stars = 0;
/*     */   private Integer isfinish;
/*  35 */   private int needtype = 0;
/*  36 */   private int needchapter = 0;
/*  37 */   private int needpagelist = 0;
/*  38 */   private int page = 1;
/*     */   private Iterator<BookInfo> it;
/*     */   private String filename;
/*  45 */   private int i = 0;
/*     */ 
/*     */   public Integer getBooktype()
/*     */   {
/*  60 */     return this.booktype;
/*     */   }
/*     */ 
/*     */   public void setBooktype(Integer booktype)
/*     */   {
/*  65 */     this.booktype = booktype;
/*     */   }
/*     */ 
/*     */   public int getNeedpagelist()
/*     */   {
/*  70 */     return this.needpagelist;
/*     */   }
/*     */ 
/*     */   public void setNeedpagelist(int needpagelist)
/*     */   {
/*  75 */     this.needpagelist = needpagelist;
/*     */   }
/*     */ 
/*     */   public Integer getIsfinish()
/*     */   {
/*  80 */     return this.isfinish;
/*     */   }
/*     */ 
/*     */   public void setIsfinish(Integer isfinish)
/*     */   {
/*  85 */     this.isfinish = isfinish;
/*     */   }
/*     */ 
/*     */   public int getNeedtype()
/*     */   {
/*  90 */     return this.needtype;
/*     */   }
/*     */ 
/*     */   public void setNeedtype(int needtype)
/*     */   {
/*  95 */     this.needtype = needtype;
/*     */   }
/*     */ 
/*     */   public int getNeedchapter()
/*     */   {
/* 100 */     return this.needchapter;
/*     */   }
/*     */ 
/*     */   public void setNeedchapter(int needchapter)
/*     */   {
/* 105 */     this.needchapter = needchapter;
/*     */   }
/*     */ 
/*     */   public int getStars()
/*     */   {
/* 110 */     return this.stars;
/*     */   }
/*     */ 
/*     */   public void setStars(int stars)
/*     */   {
/* 115 */     this.stars = stars;
/*     */   }
/*     */ 
/*     */   public String getVar()
/*     */   {
/* 120 */     return this.var;
/*     */   }
/*     */ 
/*     */   public void setVar(String var)
/*     */   {
/* 125 */     this.var = var;
/*     */   }
/*     */ 
/*     */   public int getNum()
/*     */   {
/* 130 */     return this.num;
/*     */   }
/*     */ 
/*     */   public void setNum(int num) {
/* 134 */     this.num = num;
/*     */   }
/*     */ 
/*     */   public int getStart()
/*     */   {
/* 140 */     return this.start;
/*     */   }
/*     */ 
/*     */   public void setStart(int start)
/*     */   {
/* 145 */     this.start = start;
/*     */   }
/*     */ 
/*     */   public String getOrder()
/*     */   {
/* 150 */     return this.order;
/*     */   }
/*     */ 
/*     */   public void setOrder(String order) {
/* 154 */     this.order = order;
/*     */   }
/*     */ 
/*     */   private void init()
/*     */   {
/* 162 */     String pageStr = ServletActionContext.getRequest().getParameter("page");
/* 163 */     String booktypeStr = ServletActionContext.getRequest().getParameter("type");
/* 164 */     this.filename = ServletActionContext.getRequest().getParameter("filename");
/* 165 */     if ((pageStr != null) && (!"".equals(pageStr))) {
/* 166 */       this.page = Integer.parseInt(pageStr);
/*     */     }
/*     */ 
/* 173 */     LinkedHashMap orderby = new LinkedHashMap();
/* 174 */     if ((this.order != null) && (!"".equals(this.order))) {
/* 175 */       String[] orderStr = this.order.split("\\s+");
/* 176 */       orderby.put(orderStr[0], orderStr[1]);
/*     */     }
/*     */ 
/* 181 */     String whereStr = " 1=1 ";
/* 182 */     List params = new ArrayList();
/*     */ 
/* 185 */     if ((booktypeStr != null) && (!"".equals(booktypeStr))) {
/* 186 */       this.booktype = Integer.valueOf(Integer.parseInt(booktypeStr));
/* 187 */       whereStr = whereStr + " and o.booktype=?" + (params.size() + 1);
/* 188 */       params.add(this.booktype);
/*     */     }
/* 191 */     else if (this.booktype.intValue() > 0) {
/* 192 */       whereStr = whereStr + " and o.booktype=?" + (params.size() + 1);
/* 193 */       params.add(this.booktype);
/*     */     }
/* 195 */     if (this.stars > 0) {
/* 196 */       whereStr = whereStr + " and o.stars=?" + (params.size() + 1);
/* 197 */       params.add(Integer.valueOf(this.stars));
/*     */     }
/*     */ 
/* 202 */     if (this.isfinish != null) {
/* 203 */       whereStr = whereStr + " and o.isfinish=?" + (params.size() + 1);
/* 204 */       if (this.isfinish.intValue() > 0) {
/* 205 */         params.add(Boolean.valueOf(true));
/*     */       }
/*     */       else
/*     */       {
/* 209 */         params.add(Boolean.valueOf(false));
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 218 */     boolean needBookType = false;
/* 219 */     boolean needLastChapter = false;
/* 220 */     if (this.needtype > 0) {
/* 221 */       needBookType = true;
/*     */     }
/*     */ 
/* 224 */     if (this.needchapter > 0) {
/* 225 */       needLastChapter = true;
/*     */     }
/*     */ 
/* 249 */     int firstindex = (this.page - 1) * this.num + this.start;
/*     */ 
/* 251 */     WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());
/* 252 */     BookInfoService bookService = (BookInfoService)wac.getBean("bookInfoServiceBean");
/* 253 */     QueryResult qr = bookService.getScrollData(firstindex, this.num, whereStr, params.toArray(), orderby);
/*     */ 
/* 256 */     if (((this.booktype != null) && (this.booktype.intValue() > 0)) || ((this.filename != null) && (!"".equals(this.filename))) || (this.needpagelist > 0)) {
/* 257 */       PageView pageView = new PageView(this.num, this.page);
/* 258 */       pageView.setTotalrecords((int)qr.getTotalrecord());
/*     */ 
/* 262 */       String pagelist = generatePageInfo(pageView);
/* 263 */       this.pageContext.setAttribute("pagelist", pagelist);
/*     */     }
/*     */ 
/* 270 */     List bookInfos = qr.getResultlist();
/*     */ 
/* 272 */     bookService.setInfo(bookInfos, needBookType, needLastChapter);
/* 273 */     this.it = bookInfos.iterator();
/*     */   }
/*     */ 
/*     */   private String generatePageInfo(PageView<BookInfo> pageView)
/*     */   {
/* 290 */     if ((pageView == null) || (pageView.getTotalpage() == 0L)) {
/* 291 */       return "";
/*     */     }
/* 293 */     int firstPageIndex = pageView.getPageindex().getFirstindex();
/* 294 */     int lastPageIndex = pageView.getPageindex().getLastindex();
/* 295 */     int currentPage = pageView.getCurrentpage();
/* 296 */     StringBuilder sb = new StringBuilder();
/* 297 */     sb.append("<span>共" + pageView.getTotalrecords() + "条记录 页次:" + currentPage + "/" + pageView.getTotalpage() + "页</span>");
/*     */ 
/* 303 */     PropertiesUtil properUtil = new PropertiesUtil("config.properties");
/*     */     String addStr;
/*     */     String pageUrl;
/* 306 */     if ("0".equals(properUtil.getProperty("urlwrite_tag")))
/*     */     {
/* 308 */       if ("selfdefine".equals(ServletActionContext.getContext().getName()))
/* 309 */         pageUrl = "/selfdefine?temname=" + ServletActionContext.getRequest().getParameter("temname");
/*     */       else {
/* 311 */         pageUrl = "/list?type=" + this.booktype + "&filename=" + this.filename;
/*     */       }
/* 313 */       addStr = "&page=";
/*     */     }
/*     */     else
/*     */     {
/* 317 */       if ("selfdefine".equals(ServletActionContext.getContext().getName())) {
/* 318 */         pageUrl = "/self/" + ServletActionContext.getRequest().getParameter("temname");
/*     */       }
/*     */       else
/*     */       {
/* 320 */         if ("1".equals(properUtil.getProperty("url_mode")))
/*     */         {
/* 322 */           pageUrl = "/list/" + this.booktype;
/*     */         }
/* 324 */         else pageUrl = "/" + this.filename;
/*     */       }
/*     */ 
/* 327 */       addStr = "/";
/*     */     }
/* 329 */     if (currentPage == 1)
/* 330 */       sb.append("<span class=\"disable\">首页</span><span class=\"disable\">上一页</span>");
/*     */     else {
/* 332 */       sb.append("<a href=\"" + pageUrl + addStr + "1" + "\">首页</a><a href=\"" + pageUrl + addStr + (currentPage - 1) + "\">上一页</a>");
/*     */     }
/* 334 */     for (int pageindex = firstPageIndex; pageindex <= lastPageIndex; pageindex++) {
/* 335 */       if (pageindex == currentPage) {
/* 336 */         sb.append("<span class=\"disable\">" + pageindex + "</span>");
/*     */       }
/*     */       else {
/* 339 */         sb.append("<a href=\"" + pageUrl + addStr + pageindex + "\">" + pageindex + "</a>");
/*     */       }
/*     */ 
/*     */     }
/*     */ 
/* 344 */     if (currentPage != pageView.getTotalpage()) {
/* 345 */       sb.append("<a href=\"" + pageUrl + addStr + (currentPage + 1) + "\">下一页</a><a href=\"" + pageUrl + addStr + pageView.getTotalpage() + "\">尾页</a>");
/*     */     }
/*     */ 
/* 349 */     return sb.toString();
/*     */   }
/*     */ 
/*     */   public int doStartTag()
/*     */     throws JspException
/*     */   {
/* 358 */     init();
/*     */ 
/* 361 */     this.pageContext.getResponse().setCharacterEncoding("utf-8");
/* 362 */     if ((this.it != null) && (this.it.hasNext())) {
/* 363 */       this.i += 1;
/* 364 */       this.pageContext.setAttribute(this.var, this.it.next());
/* 365 */       this.pageContext.setAttribute("i", Integer.valueOf(this.i));
/* 366 */       return 1;
/*     */     }
/* 368 */     return 0;
/*     */   }
/*     */ 
/*     */   public int doAfterBody() throws JspException
/*     */   {
/* 373 */     if ((this.it != null) && (this.it.hasNext())) {
/* 374 */       this.i += 1;
/* 375 */       this.pageContext.setAttribute(this.var, this.it.next());
/* 376 */       this.pageContext.setAttribute("i", Integer.valueOf(this.i));
/* 377 */       return 2;
/*     */     }
/* 379 */     return 0;
/*     */   }
/*     */ 
/*     */   public int doEndTag()
/*     */     throws JspException
/*     */   {
/* 386 */     this.i = 0;
/* 387 */     this.pageContext.removeAttribute(this.var);
/* 388 */     this.pageContext.removeAttribute("i");
/* 389 */     return 6;
/*     */   }
/*     */ }

/* Location:           C:\work\apache-tomcat-7.0.55\webapps\ROOT\WEB-INF\classes\
 * Qualified Name:     com.wzh.action.tag.BookTag
 * JD-Core Version:    0.6.1
 */